# -*- coding:utf-8 -*-

import re
import sys, os, time, math
from PySide6 import QtCore, QtWidgets, QtGui


class LoadWidget(QtWidgets.QDialog):
    def __init__(self, endObj):
        super().__init__()
        self.endObj = endObj
        self.main_layout = QtWidgets.QVBoxLayout(self)
        # 内容居左
        self.setStyleSheet("background-color: #fff;")
        self.testWidght = QtWidgets.QWidget()
        self.testWidght.setStyleSheet("background-color: #fffff0;")
        self.testWidght_layout = QtWidgets.QVBoxLayout(self.testWidght)
        self.testWidght_layout.setContentsMargins(0, 0, 0, 0)
        # self.testWidght_layout.addWidget(QtWidgets.QLabel("正在升级, 请勿点击"))
        self.prosse_layout = QtWidgets.QHBoxLayout()
        # QtWidgets.QPushButton(self.testWidght,"测试")
        # 设置大小
        self.testWidght.setFixedSize(480, 30)
        self.prosse_layout.addWidget(self.testWidght)

        self.title_lable = QtWidgets.QLabel("正在升级,请稍后...")
        # 设置字体大小
        self.title_lable.setFont(QtGui.QFont("黑体", 10))
        # 设置高度
        self.title_lable.setFixedHeight(25)
        # 设置下边距
        self.title_lable.setContentsMargins(0, 0, 0, 10)
        # 居中显示
        self.main_layout.setAlignment(QtCore.Qt.AlignCenter)
        self.main_layout.addWidget(self.title_lable)
        self.main_layout.addLayout(self.prosse_layout)

        self.speval = 0
        self.prosse_len = 480
        self.resize(520, 90)
        # # 设定不可更改大小
        self.setFixedSize(self.width(), self.height())
        # # 不显示标题栏
        self.setWindowFlags(QtCore.Qt.CustomizeWindowHint | QtCore.Qt.WindowStaysOnTopHint)
        self.set_pross(0)
        self.show()

    def set_pross(self, speval):
        if (speval == 0):
            # 创建一个进度条
            # self.prosse = QtWidgets.QLabel()
            self.prosse = QtWidgets.QPushButton(self.testWidght, "测试")
            self.prosse.resize(30, 0)
            # 设置背景色
            self.prosse.setStyleSheet("background-color: rgba(133, 122, 144, 0.5);")
            self.pross_animation = QtCore.QPropertyAnimation(self.prosse, b'size')
            self.pross_animation.finished.connect(self.finished_animation)
            self.pross_height = 30
            self.pross_status = QtCore.QSize(0, self.pross_height)

        self.pross_num = math.ceil(self.prosse_len * speval);
        if (self.pross_num > self.prosse_len):
            self.pross_num = 490

        self.pross_animation.setStartValue(self.pross_status)
        self.pross_status = QtCore.QSize(self.pross_num, 30)
        self.pross_animation.setEndValue(self.pross_status)
        self.pross_animation.setDuration(1000)
        self.pross_animation.setEasingCurve(QtCore.QEasingCurve.OutSine)
        self.pross_animation.start()
        self.speval = speval

    def finished_animation(self):
        self.prosse.setStyleSheet("background-color: green;")
        if (self.speval < 1):
            # todo 获取相应的数据,传值给进度条
            # if self.speval > 0.3:
            #     self.set_pross(0.5)
            # else:
            #     self.set_pross(self.speval + 0.1)
            self.set_pross(self.speval + 0.1)

        else:
            print("动画执行完毕")
            self.prosse.setStyleSheet("background-color: rgba(255, 255, 255, 0);")
            self.close()

    def closeEvent(self, event):
        print("--------------> pross end1")


class MyWidget_test(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.right_layout = QtWidgets.QVBoxLayout()
        self.button = QtWidgets.QPushButton("开始升级")
        self.button1 = QtWidgets.QPushButton("取消升级")
        self.right_layout.addWidget(self.button)
        self.right_layout.addWidget(self.button1)

        self.button.clicked.connect(self.click_button)
        self.button1.clicked.connect(self.click_button1)

        # 创建子窗口
        main_widght = QtWidgets.QWidget(self)
        main_widght.setLayout(self.right_layout)

        self.setCentralWidget(main_widght)
        self.resize(700, 500)
        self.setWindowOpacity(0.9)  # 只有设置透明度不为1的时候才会穿透
        self.show()

    def click_button1(self):
        self.loadWidget.close()
        self.loadWidget.destroy()

    def click_button(self):
        self.loadWidget = LoadWidget(self)

    def closeEvent(self, event):
        print("--------------> pross end2")
        self.loadWidget.close()


if __name__ == '__main__':
    app = QtWidgets.QApplication([])
    widget = MyWidget_test()
    widget.setWindowTitle("升级")
    sys.exit(app.exec())
